import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NavigatorUtils {
  ///[content] 上下文对象
  ///[targetPage] 目标页面
  ///[isReplace] 是否替换当前页面
  static void pushPage(
      {@required BuildContext context,
      @required Widget targetPage,
      bool isReplace = false,
      Function(dynamic value) dismissCallback}) {
    PageRoute pageRoute;
    if (Platform.isAndroid) {
      pageRoute = MaterialPageRoute(builder: (BuildContext context) {
        return targetPage;
      });
    } else {
      pageRoute = CupertinoPageRoute(builder: (BuildContext context) {
        return targetPage;
      });
    }
    if (isReplace) {
      Navigator.of(context).pushReplacement(pageRoute).then((value) {
        if (dismissCallback != null) {
          dismissCallback(value);
        }
      });
    } else {
      Navigator.of(context).push(pageRoute).then((value) {
        if (dismissCallback != null) {
          dismissCallback(value);
        }
      });
    }
  }

  ///[context]
  ///[targetPage]
  ///[isReplace]
  ///[opaque] 如果为false 则为透明背景，如果为true，则为黑白背景
  ///[dismissCallback]

  static void pushPageByFade(
      {@required BuildContext context,
      @required Widget targetPage,
      bool isReplace = false,
      bool opaque = false,
      int startMills = 400,
      Function(dynamic value) dismissCallback}) {
    PageRoute pageRoute = PageRouteBuilder(
      opaque: opaque,
      pageBuilder: (BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return targetPage;
      },
      transitionDuration: Duration(milliseconds: startMills),
      transitionsBuilder: (BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation, Widget child) {
        return FadeTransition(opacity: animation, child: child);
      },
    );
    if (isReplace) {
      Navigator.of(context).pushReplacement(pageRoute).then((value) {
        if (dismissCallback != null) {
          dismissCallback(value);
        }
      });
    } else {
      Navigator.of(context).push(pageRoute).then((value) {
        if (dismissCallback != null) {
          dismissCallback(value);
        }
      });
    }
  }
}
